package lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;

public class LockController {
    public static void main(String[] args) {
        Data2 data = new Data2();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
    }

}

class Data2 {
    private Lock lock = new ReentrantLock();
    private Condition con1 = lock.newCondition();
    private Condition con2 = lock.newCondition();
    private Condition con3 = lock.newCondition();
    private int num = 1;
    public void printA() throws InterruptedException {
        lock.lock();
        while (num!=1){
            con1.await();
        }
        num = 2;
        System.out.println(Thread.currentThread().getName()+"->"+"AAAAAA");
        con2.signal();
    }
    public void printB() throws InterruptedException {
        lock.lock();
        while (num!=2){
            con2.await();
        }
        num = 3;
        System.out.println(Thread.currentThread().getName()+"->"+"BBBBB");
        con3.signal();
    }
    public void printC() throws InterruptedException {
        lock.lock();
        while (num!=3){
            con3.await();
        }
        num = 1;
        System.out.println(Thread.currentThread().getName()+"->"+"CCCCCCC");
        con1.signal();
    }

}
